﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ControleRepresentante.Logic.Models;
using WebSite.ViewModels.DetailsViewModels.DetailsGridViewModels;
using WebSite.ViewModels.Fields;

namespace WebSite.ViewModels.DetailsViewModels
{
    public class ClienteDetailsViewModel : UsuariosHolderDetailsViewModel
    {
        private readonly bool m_CanViewVendedor;

        public ClienteDetailsViewModel(bool canViewVendedor) 
            : base("Cliente")
        {
            m_CanViewVendedor = canViewVendedor;
        }

        protected override IEnumerable<IField> GetGeralFields()
        {
            Cliente cliente = (Cliente)Model;

            IField vendedorField;
            if (cliente.Vendedor == null)
            {
                vendedorField = new LabelField("Vendedor", "vendedor", "Nenhum");
            }
            else if (m_CanViewVendedor)
            {
                vendedorField = new LinkField("Vendedor", "vendedor", cliente.VendedorNome, "Details", "Vendedor", new {id = cliente.Vendedor.Id});
            }
            else
            {
                vendedorField = new LabelField("Vendedor", "vendedor", cliente.VendedorNome);
            }
            List<IField> result = new List<IField>
            {
                new LabelField("Razão Social", "razaoSocial", cliente.RazaoSocial),
                new LabelField("Nome Fantasia", "nomeFantasia", cliente.NomeFantasia),
                vendedorField,
                new LabelField("CNPJ", "cnpj", cliente.CNPJ),
                new LabelField("Inscrição Estadual", "inscricaoEstadual", cliente.InscricaoEstadual),
                new LabelField("Cliente Desde", "clienteDesde", cliente.ClienteDesde),
            };
            result.AddRange(base.GetGeralFields());
            return result;
        }

        protected override IDetailsGridViewModel GetEnderecoDisplayGrid(HtmlHelper htmlHelper, DetailsHolder detailsHolder)
        {
            Cliente cliente = (Cliente)detailsHolder;
            IEnumerable<ClienteEndereco> clienteEnderecos = 
                cliente.Enderecos.Select(x => new ClienteEndereco(x, cliente));
            return new ClienteEnderecoDetailsGridViewModel(htmlHelper, clienteEnderecos);
        }

        public override IEnumerable<ButtonType> AdditionalButtons => new[] {ButtonType.Print};
    }
}